TOOL.Category		= "Cosmos"
TOOL.Name			= "#Link"
TOOL.Command		= nil
TOOL.ConfigName		= nil

if( CLIENT ) then
	
	language.Add( "Tool_cosmos_link_name", "Link" );
	language.Add( "Tool_cosmos_link_desc", "Link two generators/containers/dispensers together." );
	language.Add( "Tool_cosmos_link_0", "Left click on a cosmic entity to add a link. Right click on one to clear links." );
	language.Add( "Tool_cosmos_link_1", "Click on what you want to link it to." );
	
end

function TOOL:LeftClick( tr )
	
	if( !tr.Entity.Cosmic ) then return end
	
	if( CLIENT ) then return true end
	
	local n = self:NumObjects()

	local Phys = tr.Entity:GetPhysicsObjectNum( tr.PhysicsBone )
	self:SetObject( n + 1, tr.Entity, tr.HitPos, Phys, tr.PhysicsBone, tr.HitNormal )

	if( n > 0 ) then
		
		local e1, e2 = self:GetEnt( 1 ), self:GetEnt( 2 );
		
		if( e1 == e2 ) then return true end
		
		if( !e1.Links ) then C.ClearLinks( e1 ); end
		if( !e2.Links ) then C.ClearLinks( e2 ); end
		
		C.Link( e1, e2 );
		
		self:ClearObjects();

	else
	
		self:SetStage( n + 1 );
		
	end

	return true
	
end

function TOOL:RightClick( tr )
	
	if( !tr.Entity.Cosmic ) then return end
	
	if( CLIENT ) then return true end
	
	C.ClearLinks( tr.Entity );
	
	return true
	
end

function TOOL:Reload( tr )
	
	return true
	
end
